using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrackSystem.IO.Data;
using TrackSystem.IO.Models;

namespace TrackSystem.IO.Protocol
{
    /// <summary>
    /// 二号船电罗经通信协议 
    /// 0--heading;1--rate
    /// </summary>
    public class Ship2GyroProtocol : ProtocolBase
    {
        private int startTicks;
        private bool bSendR;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public Ship2GyroProtocol(string name)
            : base()
        {
            NeedInit = false;
            Name = name;
            bSendR = false;
        }

        /// <summary>
        /// 生成读取寄存器的通讯帧
        /// </summary>
        /// <returns></returns>
        public override byte[] GenerateReadFrame(out int NeedRecvLen)
        {
            startTicks = Environment.TickCount;
            if (bSendR)
            {
                Fail_R++;
                // 失败超过10次，成功次数清零
                if (Fail_R >= 10)
                {
                    Succ_R = 0;
                }
            }
            bSendR = true;
            NeedRecvLen = 40;
            return null;
        }


        /// <summary>
        /// 根据收到的内容进行处理
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public override bool ProcessReadFrame(byte[] buffer)
        {
            Rx_r = buffer;
            Lapse = Environment.TickCount - startTicks;

            try
            {
                string frame = Encoding.UTF8.GetString(buffer);
                string[] ar_line = frame.Split('\n');
                bool bHead = true;
                bool bRot = true;
                try {
                    for (int i = 0; i < ar_line.Length; i++)
                    {
                        string[] ar_str = ar_line[i].Split(',');
                        if (bHead && ar_str.Length >= 3 && ar_str[0].Trim() == "$HEHDT")
                        {
                            RegFactory.Instance.SetVal(RegNameList[0], double.Parse(ar_str[1]));
                            bHead = false;
                        }

                        if (bRot && ar_str.Length >= 3 && ar_str[0].Trim() == "$HEROT")
                        {
                            RegFactory.Instance.SetVal(RegNameList[1], double.Parse(ar_str[1]));
                            bRot = false;
                        }
                        if (!bHead && !bRot) {
                            break;
                        }
                    }
                    if (!bHead || !bRot)
                    {
                        Succ_R++;
                        bSendR = false;     // 正确收到，标志位变为false
                        Fail_R = 0;         // 通信成功，则将失败次数清零
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
